﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using ProxyViewModel.Attributes;
using ProxyViewModel;
using ProxyViewModel.Messaging;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Threading;
namespace AutoViewModel.Tests {
	[TestClass]
	public class MessagingTest {
		public class ViewModelA {
			protected bool _acceptMessageCalled = false;
			protected string _message1Recieved = string.Empty;
			protected string _message2Recieved = string.Empty;

			public string Message2Recieved {
				get { return _message2Recieved; }
			}

			public string Message1Recieved {
				get { return _message1Recieved; }
			}

			public bool AcceptMessageCalled {
				get { return _acceptMessageCalled; }
			}

			[MessageReciever(OnNewThread = true)]
			public void AcceptMessage(string message1, string message2) {
				_acceptMessageCalled = true;
				_message1Recieved = message1;
				_message2Recieved = message2;
			}

			public virtual void Reset() {
				_acceptMessageCalled = false;
				_message1Recieved = string.Empty;
				_message2Recieved = string.Empty;
			}
		}

		public class ViewModelB : ViewModelA {
			private bool _acceptMessageCalled2 = false;

			public bool AcceptMessageCalled2 {
				get { return _acceptMessageCalled2; }
			}

			[MessageReciever(OnNewThread = true)]
			public void AcceptMessage(string message1) {
				_acceptMessageCalled2 = true;
				_message1Recieved = message1;
			}

			public override void Reset() {
				base.Reset();
				_acceptMessageCalled2 = false;
			}
		}

		[TestMethod]
		public void TestMessageIsTunneled() {
			ViewModelA viewModel = ProxyViewModelFactory.CreateProxyViewModel<ViewModelA>();
			Messaging.Publish("Parameter1", "Parameter2");
			Thread.Sleep(100);
			Assert.IsTrue(viewModel.AcceptMessageCalled);
			Assert.AreEqual(viewModel.Message1Recieved, "Parameter1");
			Assert.AreEqual(viewModel.Message2Recieved, "Parameter2");
		}


		[TestMethod]
		public void TextComplexScenario1() {
			ViewModelB viewModelB = ProxyViewModelFactory.CreateProxyViewModel<ViewModelB>();
			ViewModelA viewModelA = ProxyViewModelFactory.CreateProxyViewModel<ViewModelA>();

			Messaging.Publish("Parameter1", "Parameter2");
			Thread.Sleep(100);

			Assert.IsTrue(viewModelB.AcceptMessageCalled);
			Assert.AreEqual(viewModelB.Message1Recieved, "Parameter1");
			Assert.AreEqual(viewModelB.Message2Recieved, "Parameter2");
			Assert.IsFalse(viewModelB.AcceptMessageCalled2);

			Assert.IsTrue(viewModelA.AcceptMessageCalled);
			Assert.AreEqual(viewModelA.Message1Recieved, "Parameter1");
			Assert.AreEqual(viewModelA.Message2Recieved, "Parameter2");

			viewModelA.Reset();
			viewModelB.Reset();

			Messaging.Publish("parameter");

			Thread.Sleep(100);

			Assert.IsFalse(viewModelB.AcceptMessageCalled);
			Assert.AreEqual(viewModelB.Message1Recieved, "parameter");
			Assert.AreEqual(viewModelB.Message2Recieved, string.Empty);
			Assert.IsTrue(viewModelB.AcceptMessageCalled2);

			Assert.IsFalse(viewModelA.AcceptMessageCalled);
			Assert.AreEqual(viewModelA.Message1Recieved, string.Empty);
			Assert.AreEqual(viewModelA.Message2Recieved, string.Empty);
		}
	}
}
